package cn.curtain.girl.controller;

import cn.curtain.girl.domain.Girl;
import cn.curtain.girl.domain.Result;
import cn.curtain.girl.domain.User;
import cn.curtain.girl.repository.GirlRepository;
import cn.curtain.girl.service.GirlService;
import cn.curtain.girl.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Role;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class GirlControl {

    @Autowired
    private GirlRepository girlRepository;

    @Autowired
    private GirlService girlService;

    /*
     *查询所有列表
     */
    @PreAuthorize("hasRole('ROLE_USER')")
    @GetMapping(value = "/girls")
    public List<Girl> girlList(@AuthenticationPrincipal User user) {

//        System.out.println("测试Aspect执行时间 _girlList");
        return girlRepository.findAll();
    }

    //添加一条记录
    @PostMapping(value = "/girls")
    public Girl girlAdd(@RequestParam("cupSize") String cupSize,
                        @RequestParam("age") Integer age) {
        Girl girl = new Girl();
        girl.setCup(cupSize);
        girl.setAge(age);

        return girlRepository.save(girl);
    }

    //参数过多的时候  传一个对象    @Valid 会根据设定的条件 对数据进行验证
    //BindingResult 对象中保存着验证的结果
    @PostMapping(value = "/girls2")
    public Result<Girl> girlAdd2(@Valid Girl girl, BindingResult bindingResult){


        if(bindingResult.hasErrors()){
            return ResultUtil.fail(1,bindingResult.getFieldError().getDefaultMessage());
        }

        girl.setAge(girl.getAge());
        girl.setCup(girl.getCup());
        return ResultUtil.success(girlRepository.save(girl));
    }

    // 根据查询一条记录
//    @PreAuthorize("#token==""admin")
    @GetMapping(value = "/girls/{id}")
    public Girl selectGirl(@PathVariable("id") Integer id) {
        return girlRepository.findOne(id);
    }

    //更新一条记录
    @PutMapping(value = "/girls/{id}")
    public Girl girlUpdate(@RequestParam("id") Integer id,
                           @RequestParam("cupSize") String cupSize,
                           @RequestParam("age") Integer age) {
        Girl girl = new Girl();
        girl.setId(id);
        girl.setCup(cupSize);
        girl.setAge(age);

        return girlRepository.save(girl);
    }

    //删除一条记录
    @DeleteMapping(value = "/girls/{id}")
    public void girlDelete(@PathVariable("id") Integer id){
        girlRepository.delete(id);
    }

    //通过年龄查询女生列表
    @GetMapping(value = "/girls/age/{age}")
    public List<Girl> girlListByAge(@PathVariable("age") Integer age){
        return girlRepository.findByAge(age);
    }

    @GetMapping(value = "/girls/getAge/{id}")
    public void getAge(@PathVariable Integer id)throws Exception{
        girlService.getAge(id);
    }

    //测试事务操作
    @PostMapping(value = "/girls/two")
    public void girlTwo(){
        girlService.insertTwo();
    }

}
